var database = require('../../../data/data.js')
var QQMapWX = require('../../../libs/qqmap-wx-jssdk.js');
var qqmapsdk;
var app = getApp();
Page({
  data: {
    flag: true,
    flag1: true,
    flag2: true,
    flag3: true,
    choiceFlag: false,
    moneyFlag: false,
    team_tip: "",
    date_tip: "",
    time_tip: "",
    address_tip: "",
    people_tip: "",
    turf_tip: "",
    cost_tip: "",
    team: [],
    money: "",
    ensure: "",
    award:"" ,
    clothing: "",
    notes: "",
    week: "",
    people: ['11人', '10人', '9人', '8人', '7人', '6人', '5人', '4人', '3人'],
    turf: ['人造草', '天然草', '木地板'],
    cost: ['对手免费', 'AA制'],
    check_img: [
      { img: "../../../image/icon/nochoice.png" },
      { img: "../../../image/icon/nochoice.png" },
    ]
  },
  onLoad: function (options) {
    var that = this;
    wx.showLoading({
      title: '资源加载中',
      mask: true
    })
    qqmapsdk = new QQMapWX({
      key: 'NRIBZ-OFHHR-DZMWB-W5EIN-FQXME-E5FC2'
    });
    this.setData({
      openid: database.userId
    })
    // wx.request({
    //   url: database.url1 + '/api/V2/user/currency',
    //   data: {
    //     accessToken: database.accessToken,
    //     userId: database.userId
    //   },
    //   method: "POST",
    //   header: {
    //     'content-type': 'application/x-www-form-urlencoded'
    //   },
    //   success(res) {
    //     that.setData({
    //       balance: res.data.data.currency
    //     })
    //   }
    // });
    wx.request({
      url: database.url + '/project/getContestDetail.action',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
      data: {
        openid: options.popenid,
        id: options.gameid
      },
      success(res) {
        var  contest = res.data.contest;
        var date = new Date(contest.postdate);
        var date1 = new Date();
        var seconds = 0;
        var minutes = 0;
        var hour = 0;
        var day = 0;
        var month = 0;
        var year = 0;
        var timeDifference = "";
        var contact = contest.contact;
        contact = contact.substring(0, 3) + "****" + contact.substring(7);
        wx.request({
          url: database.url1 + '/api/V2/softball/get_softball',
          data: {
            accessToken: database.accessToken,
            userId: contest.popenid,
            softballId: contest.pteamid
          },
          method: "POST",
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success(res) {
            wx.hideLoading();
            that.setData({
              timeDifference: timeDifference,
              data: contest,
              contact: contact,
              teamName:res.data.data.title  
            });
          }
        });
      }
    });
    wx.request({
      url: database.url1 + '/api/V2/softball/softball_list',
      data: {
        accessToken: database.accessToken,
        userId: database.userId
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        var teamList = res.data.data.softballList;
        var team = [];
        for (var i = 0; i < teamList.length; i++) {
          team[i] = teamList[i].title
        }
        wx.hideLoading();
        that.setData({
          team: team,
          teamList: teamList
        })
      }
    });
  },
  bindTeamChange: function (e) {
    this.setData({
      team_tip: "none",
      team_index: e.detail.value
    })
  },
  bindDateChange: function (e) {
    var date = new Date(e.detail.value);
    var week;
    if (date.getDay() == 0) week = "星期日"
    if (date.getDay() == 1) week = "星期一"
    if (date.getDay() == 2) week = "星期二"
    if (date.getDay() == 3) week = "星期三"
    if (date.getDay() == 4) week = "星期四"
    if (date.getDay() == 5) week = "星期五"
    if (date.getDay() == 6) week = "星期六"
    this.setData({
      date_tip: "none",
      date: e.detail.value,
      week: week
    })
  },
  bindTimeChange: function (e) {
    this.setData({
      time_tip: "none",
      time: e.detail.value
    })
  },
  bindPeopleChange: function (e) {
    this.setData({
      people_tip: "none",
      people_index: e.detail.value
    })
  },
  bindTurfChange: function (e) {
    this.setData({
      turf_tip: "none",
      turf_index: e.detail.value
    })
  },
  hide: function () {
    this.setData({
      flag: true,
      cost_tip: ""
    })
  },
  // hide1: function () {
  //   this.setData({
  //     flag3: true
  //   })
  // },
  change_img: function (e) {
    this.setData({
      choiceFlag: true
    })
    if (e.currentTarget.dataset.check == 1) {
      this.setData({
        flag1: false
      })
    } else {
      this.setData({
        flag1: true
      })
    }
    var check = e.currentTarget.dataset.check
    var check_img = [
      { img: "../../../image/icon/nochoice.png" },
      { img: "../../../image/icon/nochoice.png" },
    ]
    check_img[check].img = "../../../image/icon/choice.png"
    this.setData({
      check_img: check_img
    })
  },
  money_change: function (e) {
    if (e.detail.value != "") {
      this.setData({
        money: e.detail.value,
        moneyFlag: true
      })
    } else {
      this.setData({
        moneyFlag: false
      })
    }
  },
  choice_ok: function (e) {
    if (this.data.flag1 == true) {
      this.setData({
        money: "对手免费",
        flag: true,
        cost_tip: "none"
      })
    }
    if (this.data.flag2 == true) {
      if (this.data.money != "") {
        this.setData({
          flag: true,
          cost_tip: "none"
        })
      }
    }
  },
  bindCostChange: function (e) {
    this.setData({
      money: "",
      flag: false
    })
  },
  fight: function (e) {
    wx.redirectTo({
      url: '/pages/my/my_add_fight/my_add_fight',
    })
  },
  home: function (e) {
    wx.redirectTo({
      url: '/pages/home/home',
    })
  },
  choiceAddress: function () {
    var that = this;
    wx.chooseLocation({
      success(res) {
        that.setData({
          address_tip: "none",
          address: res.name,
          lat: res.latitude,
          lng: res.longitude
        })
      }
    })
  },
  // ensureChange: function (e) {
  //   if (e.detail.value != "") {
  //     this.setData({
  //       ensure: e.detail.value
  //     })
  //   }
  // },
  // awardChange: function (e) {
  //   if (e.detail.value != "") {
  //     this.setData({
  //       award: e.detail.value
  //     })
  //   }
  // },
  clothingChange: function (e) {
    if (e.detail.value != "") {
      this.setData({
        clothing: e.detail.value
      })
    }
  },
  notesChange: function (e) {
    if (e.detail.value != "") {
      this.setData({
        notes: e.detail.value
      })
    }
  },
  submit: function (e) {
    var that = this;
    var data = e.detail.value;
    // if (this.data.balance > e.detail.value.award - this.data.data.award) {
    var gameid = this.data.data.gameid;
    var teamid ='';
    if (this.data.team_tip == '') {
      teamid = this.data.data.pteamid;
    }else {
      teamid = this.data.teamList[this.data.team_index].id;
    }
    var date = data.date;
    if (this.data.date_tip == ''){
      date = this.data.data.date;
    }
    var week = this.data.week;
    if (this.data.date_tip == ''){
      week = this.data.data.week;
    }
    var time = data.time;
    if (this.data.time_tip == ''){
      time = this.data.data.time;
    }
    var adress = this.data.address;
    var lat = this.data.lat;
    var lng = this.data.lng;
    if (this.data.address_tip == ''){
      adress = this.data.data.adress;
      lat = this.data.data.lat;
      lng = this.data.data.lng;
    }
    var people='';
    if (this.data.people_tip == '') {
      people = this.data.data.person;
    } else{
      people = data.people.substring(0, data.people.length - 1);
    }
    var clothing = data.clothing;
    if(clothing == ''){
      clothing = this.data.data.clothing;
    }
    var turf = data.turf;
    if (this.data.turf_tip == ''){
      turf = this.data.data.turf;
    }
    var money = this.data.money;
    if (this.data.cost_tip == ''){
        money = this.data.data.cost
    }
    else {
      if (money == "对手免费") {
           money = 0;
       } else {
           money = this.data.money;
      }
    }
    var ensure = this.data.ensure;
    if(ensure == ''){
      ensure = this.data.data.ensure;
    }
    var award = this.data.award;
    if (award == '') {
      award = this.data.data.award;
    }
    var notes = this.data.notes;
    if (notes == '') {
      notes = this.data.data.notes;
    }
    wx.request({
      url: database.url + '/project/uploadUpdateContest.action',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
      data: {
        id:gameid,
        pteamid: teamid,
        popenid: database.userId,
        date:date,
        week: week,
        time: time,
        lat: lat,
        lng: lng,
        adress: adress,
        contact: database.tel,
        person: people,
        clothing: clothing,
        turf: turf,
        cost: money,
        ensure: ensure,
        award: award,
        notes: notes
      },
      success(res) {
        if(res.data == 0){
          wx.showToast({
            title:'修改成功!',
          })
        }
      }
    })
    this.setData({
      flag2: false
    })
    // }else{
    //   this.setData({
    //     flag3: false
    //   })
    // }
  },
  // toRecharge: function () {
  //   wx.navigateTo({
  //     url: '/pages/my/recharge/recharge',
  //     flag3: true
  //   })
  // }
})